package soar.template.springboot.mq.rabbitmq.consumer.configuration.rabbitmq;

import javax.annotation.Resource;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory.CacheMode;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import soar.template.springboot.mq.rabbitmq.consumer.app.MessageConsumer;
import soar.template.springboot.mq.rabbitmq.consumer.appconfig.AppConfig;
import soar.template.springboot.mq.rabbitmq.consumer.appconfig.rabbitmq.ConsumerConfig;

@Configuration
class MyRabbitMQConfiguration
{
    @Resource(name = "appconfig")
    private AppConfig appconfig;

    @Bean
    public ConnectionFactory consumerMainConnectionFactory()
    {
        ConsumerConfig config = appconfig.rabbitmq.consumerMain;
        com.rabbitmq.client.ConnectionFactory orig_factory = new com.rabbitmq.client.ConnectionFactory();
        orig_factory.setHost(config.host);
        orig_factory.setPort(config.port);
        orig_factory.setVirtualHost(config.vhost);
        orig_factory.setUsername(config.username);
        orig_factory.setPassword(config.password);
        CachingConnectionFactory factory = new CachingConnectionFactory(orig_factory);
        factory.setRequestedHeartBeat(30);
        factory.setCacheMode(CacheMode.CONNECTION);
        factory.setConnectionLimit(10);
        return factory;
    }

    @Bean
    public DirectExchange consumerMainDirectExchange()
    {
        return new DirectExchange(appconfig.rabbitmq.consumerMain.exchange,
                true, false);
    }

    @Bean
    public Queue consumerMainQueue()
    {
        return new Queue(appconfig.rabbitmq.consumerMain.queue, true, false,
                false);
    }

    @Bean
    public Binding consumerMainBinding()
    {
        return BindingBuilder.bind(consumerMainQueue())
                .to(consumerMainDirectExchange())
                .with(appconfig.rabbitmq.consumerMain.queue);
    }

    /*--- 监听接收 ---*/
    @Resource
    private MessageConsumer messageConsumer;

    @Bean
    public MessageListenerAdapter consumerMainMessageListenerAdapter()
    {
        return new MessageListenerAdapter(messageConsumer, "handle");
    }

    @Bean
    public SimpleMessageListenerContainer consumerMainSimpleMessageListenerContainer()
    {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(consumerMainConnectionFactory());
        container.setQueueNames(appconfig.rabbitmq.consumerMain.queue);
        container.setMessageListener(consumerMainMessageListenerAdapter());
        container.setConcurrentConsumers(10);
        container.setMissingQueuesFatal(false);
        return container;
    }

}
